<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div>
        <!-- 搜索 -->
        <el-input v-model="query.applyOrderNo" clearable placeholder="输入申请单号" style="width: 150px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-input v-model="query.depositGoods" clearable placeholder="输入寄存商品名称" style="width: 150px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-input v-model="query.userName" clearable placeholder="输入客户账号" style="width: 150px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-select v-model="query.userServiceId" clearable placeholder="选择客服" class="filter-item" filterable style="width: 250px" @change="crud.toQuery">
          <el-option v-for="item in optionsServiceList" 
            :value="item.uid"
            :key="item.uid"
            :label="item.userName + ' - ('+item.nickname+')'">
          </el-option>
        </el-select>
        <el-select v-model="query.status" clearable placeholder="寄存状态" class="filter-item" style="width: 130px" @change="crud.toQuery">
          <el-option
            v-for="item in queryStatusOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-select v-model="query.applyType" clearable placeholder="申请类型" class="filter-item" style="width: 130px" @change="crud.toQuery">
          <el-option
            v-for="item in queryApplyTypeOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <rrOperation :crud="crud" />
        <!-- 新增 -->
        <el-button
          type="primary"
          class="filter-item"
          size="mini"
          icon="el-icon-open"
          :loading="statusLoading"
          :disabled="crud.selections.length === 0"
          @click="batchApplyStatus"
        >批量处理</el-button>
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <!-- <crudOperation :permission="permission" /> -->
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :visible.sync="dialogVisible" title="查看详情" width="800px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px" style="margin-top: -4%;">
          <el-collapse v-model="activeNames" @change="handleChange">
            <el-collapse-item title="申请单详情" name="1">
              <el-descriptions direction="vertical" :column="5" border>
                  <el-descriptions-item label="申请单号">{{apply.applyOrderNo}}</el-descriptions-item>
                  <el-descriptions-item label="寄存商品" :span="2">{{apply.depositGoods}}</el-descriptions-item>
                  <el-descriptions-item label="寄存天数">{{apply.depositDays}}</el-descriptions-item>
                  <el-descriptions-item label="寄存金额">{{apply.depositAmount}}</el-descriptions-item>

                  <el-descriptions-item label="寄存状态">
                    <el-tag size="small" v-if="apply.status == 0" style="color: red;">未处理</el-tag>
                    <el-tag size="small" v-else>已处理</el-tag>
                  </el-descriptions-item>
                  <el-descriptions-item label="申请类型">
                    <el-tag size="small" v-if="apply.applyType == 1">物品回收</el-tag>
                    <el-tag size="small" v-else style="color: red;">物品寄存</el-tag>
                  </el-descriptions-item>
                  <el-descriptions-item label="位置" :span="4">{{apply.address}} {{apply.detailedAddress}}</el-descriptions-item>
                  <el-descriptions-item label="寄存描述" :span="5">{{apply.introduction}}</el-descriptions-item>
              </el-descriptions>
            </el-collapse-item>
            <el-collapse-item title="客户详情" name="1">
              <el-descriptions direction="vertical" :column="4" border>
                  <el-descriptions-item label="客户账号">{{yxUser.username}}</el-descriptions-item>
                  <el-descriptions-item label="客户姓名">{{yxUser.nickname}}</el-descriptions-item>
                  <el-descriptions-item label="电话">{{yxUser.phone}}</el-descriptions-item>
                  <el-descriptions-item label="办理次数"><el-tag size="small">{{yxUser.payCount}}</el-tag></el-descriptions-item>
              </el-descriptions>
            </el-collapse-item>
            <el-collapse-item title="客服详情" name="1">
              <el-descriptions direction="vertical" :column="4" border>
                  <el-descriptions-item label="客户账号">{{syUserService.userName}}</el-descriptions-item>
                  <el-descriptions-item label="客户姓名">{{syUserService.nickname}}</el-descriptions-item>
                  <el-descriptions-item label="电话">{{syUserService.phone}}</el-descriptions-item>
                  <el-descriptions-item label="微信号"><el-tag size="small">{{syUserService.wechatNumber}}</el-tag></el-descriptions-item>
              </el-descriptions>
            </el-collapse-item>
          </el-collapse>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="dialogVisible = false">取消</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55"/>
        <el-table-column show-overflow-tooltip v-if="columns.visible('applyOrderNo')" prop="applyOrderNo" label="申请单号" width="150"/>
        <el-table-column show-overflow-tooltip v-if="columns.visible('userName')" prop="userName" label="客户账号" width="100"/>
        <el-table-column show-overflow-tooltip v-if="columns.visible('serviceName')" prop="serviceName" label="客服账号" width="100"/>
        <el-table-column show-overflow-tooltip v-if="columns.visible('depositGoods')" prop="depositGoods" label="寄存商品" width="120"/>
        <el-table-column show-overflow-tooltip v-if="columns.visible('depositDays')" prop="depositDays" label="寄存天数" width="75"/>
        <el-table-column show-overflow-tooltip v-if="columns.visible('depositAmount')" prop="depositAmount" label="寄存金额" width="75"/>
        <el-table-column show-overflow-tooltip v-if="columns.visible('introduction')" prop="introduction" label="寄存描述" width="100"/>
        <el-table-column show-overflow-tooltip v-if="columns.visible('address')" prop="address" label="省市区" width="100"/>
        <el-table-column show-overflow-tooltip v-if="columns.visible('detailedAddress')" prop="detailedAddress" label="详细地址" width="100"/>
        <el-table-column v-if="columns.visible('applyType')" prop="applyType" label="申请类型" width="80" >
          <template slot-scope="scope">
              <div>
                <el-tag v-if="scope.row.applyType == 1" :type="''">物品回收</el-tag>
                <el-tag v-else :type=" 'danger' ">物品寄存</el-tag>
              </div>
            </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('status')" prop="status" label="寄存状态" width="80" >
          <template slot-scope="scope">
              <div>
                <el-tag v-if="scope.row.status == 1" :type="''">已处理</el-tag>
                <el-tag v-else :type=" 'danger' ">未处理</el-tag>
              </div>
            </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip v-if="columns.visible('createTime')" prop="createTime" label="申请时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="100px" align="center">
          
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-search"
              @click="toDetailViews(scope.row.id)"
            >查看</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudSyDepositApply from '@/api/syDepositApply'
import { batchStatus , getApply , getServiceList } from '@/api/syDepositApply'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import MaterialList from "@/components/material";

// crud交由presenter持有
const defaultCrud = CRUD({ title: 'SyDepositApplyController', url: 'api/syDepositApply', sort: 'id,desc', crudMethod: { ...crudSyDepositApply }})
const defaultForm = { id: null, uid: null, userServiceId: null, applyOrderNo: null, depositGoods: null, depositDays: null, depositAmount: null, introduction: null, address: null, detailedAddress: null, latitude: null, longitude: null, status: null, createTime: null, updateTime: null, isDel: null, userName: null, serviceName: null }
export default {
  name: 'SyDepositApply',
  components: { pagination, crudOperation, rrOperation, udOperation ,MaterialList},
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  data() {
    return {
      optionsServiceList: [],//客服列表
      apply:{},
      syUserService:{},
      yxUser:{},
      activeNames: ['1'],
      statusLoading: false,
      dialogVisible: false,
      permission: {
        add: ['admin', 'syDepositApply:add'],
        edit: ['admin', 'syDepositApply:edit'],
        del: ['admin', 'syDepositApply:del']
      },
      queryStatusOptions: [
        { value: '0', label: '未处理' },
        { value: '1', label: '已处理' }
      ],
      queryApplyTypeOptions: [
        { value: '0', label: '物品寄存' },
        { value: '1', label: '物品回收' }
      ],
      rules: {
        applyOrderNo: [
          { required: true, message: '申请单号不能为空', trigger: 'blur' }
        ],
        depositGoods: [
          { required: true, message: '寄存商品不能为空', trigger: 'blur' }
        ],
        depositDays: [
          { required: true, message: '寄存天数不能为空', trigger: 'blur' }
        ],
        depositAmount: [
          { required: true, message: '寄存金额不能为空', trigger: 'blur' }
        ],
        address: [
          { required: true, message: '省市区不能为空', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '寄存状态（0未处理;1已处理）不能为空', trigger: 'blur' }
        ],
        createTime: [
          { required: true, message: '申请时间不能为空', trigger: 'blur' }
        ],
        isDel: [
          { required: true, message: '是否删除不能为空', trigger: 'blur' }
        ],
        userName: [
          { required: true, message: '客户账号不能为空', trigger: 'blur' }
        ],
        serviceName: [
          { required: true, message: '客服账号不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'applyOrderNo', display_name: '申请单号' },
        { key: 'depositGoods', display_name: '寄存商品' },
        { key: 'userName', display_name: '客户账号' },
        { key: 'serviceName', display_name: '客服账号' }
      ]
    }
  },
  watch: {
  },
  created() {
    let that = this;
    setTimeout(function() {
      that.getServiceListInit();
    }, 500);
  },
  methods: {
    getServiceListInit(){
      getServiceList().then(res => {
        this.optionsServiceList = res.serviceList;
      })
    },
    handleChange(val) {
      // console.log(val);
    },
    toDetailViews(id) {
      getApply(id).then((res) => {
        this.apply = res.apply;
        this.syUserService = res.syUserService;
        this.yxUser = res.yxUser;
        this.dialogVisible = true
      }).then(() => {
        this.dialogVisible = true
      })
    },
    batchApplyStatus() {
      const ids = []
      this.crud.selections.forEach(val => {
        ids.push(val.id)
      })
      this.statusLoading = true
      batchStatus(ids).then(() => {
        this.crud.refresh()
        this.crud.notify('更新成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
        this.statusLoading = false
      }).then(() => {
        this.statusLoading = false
      })
    },
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      const query = this.query
      if (query.type && query.value) {
        this.crud.params[query.type] = query.value
      }else{
        delete this.crud.params.applyOrderNo
        delete this.crud.params.depositGoods
        delete this.crud.params.userName
        delete this.crud.params.serviceName
      }
      return true
    }, // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
    },
  }
}



</script>

<style scoped>
  .table-img {
    display: inline-block;
    text-align: center;
    background: #ccc;
    color: #fff;
    white-space: nowrap;
    position: relative;
    overflow: hidden;
    vertical-align: middle;
    width: 32px;
    height: 32px;
    line-height: 32px;
  }
</style>
